/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import app.Global;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author yoshi
 */
public class Employee {

    private long id;
    private String username;
    private String password;
    private String firstName;
    private String lastName;
    private String mobile;
    private String email;
    private String address;
    private int sid;
    private boolean active;
    private long departmentId;
    private long rollId;
    private boolean gender;
    private Department department;
    private Roll roll;

    public Employee(long id) {
        this.id = id;
    }

    public Employee(ResultSet rs) {
        try {
            Department d = Global.getDepartments().get(rs.getLong("department_id"));
            Roll r = Global.getRolls().get(rs.getLong("roll_id"));
            this.id = rs.getLong("id");
            this.username = rs.getString("username");
            this.firstName = rs.getString("first_name");
            this.lastName = rs.getString("last_name");
            this.gender = rs.getBoolean("gender");
            this.address = rs.getString("address");
            this.email = rs.getString("email");
            this.mobile = rs.getString("mobile");
            this.password = rs.getString("password");
            this.department = d;
            this.departmentId = d.getId();
            this.roll = r;
            this.rollId = r.getId();
        } catch (SQLException ex) {
            Logger.getLogger(Employee.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Employee(long id, String username, String firstName, String lastName, boolean gender, Roll roll) {
        this.id = id;
        this.username = username;
        this.firstName = firstName;
        this.lastName = lastName;
        this.gender = gender;
        this.roll = roll;
        this.rollId = roll.getId();
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public boolean isGender() {
        return gender;
    }

    public void setGender(boolean gender) {
        this.gender = gender;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
        this.departmentId = department.getId();
    }

    public long getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(long departmentId) {
        if (this.department != null) {
            return;
        }
        this.departmentId = departmentId;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public long getId() {
        return id;
    }

    private void setId(long id) {
        this.id = id;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Roll getRoll() {
        return roll;
    }

    public void setRoll(Roll roll) {
        this.roll = roll;
    }

    public long getRollId() {
        return rollId;
    }

    public void setRollId(long rollId) {
        this.rollId = rollId;
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    
    public String getRollName(){
        if(this.isManager()){
            return "Manager";
        }else if(this.isAdmin()){
            if(this.isDepartmentHead()){
                return this.getRoll().getName();
            }else{
                return "Administrator";
            }
        }else{
            if(this.isDepartmentHead()){
                return this.getRoll().getName();
            }else{
                return "Doctor";
            }
        }
    }

    public String getFullName() {
        return this.getFirstName() + " " + this.getLastName();
    }

    public boolean isDepartmentHead() {
        return this.getRoll().isHead();
    }

    public boolean isManager() {
        return this.getDepartmentId() == 2;
    }

    public boolean isAdmin() {
        return this.getDepartmentId() == 1;
    }

    public String toFormatString() {
        String s = new StringBuilder().append(gender ? "Mr. " : "Ms. ").append(this.getFullName()).append(", ").append(this.getUsername()).append(", ").append(this.getRoll().getName()).toString();

        return s;
    }

    public String getFullRollName() {
        if (this.isManager()) {
            return "Manager";
        } else {
            return new StringBuilder(this.getRoll().getName()).append(" of ").append(this.getDepartment().getName()).toString();
        }
    }
    
    public String getFullTitle(){
        return new StringBuilder(gender ? "Mr. " : "Ms. ").append(this.getFullName()).append(" a.k.a ").append(this.getUsername()).toString();
    }
}
